package csu.web.mypetstore.persistence.impl;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.CartItem;
import csu.web.mypetstore.persistence.CartDao;
import csu.web.mypetstore.persistence.DBUtil;
import csu.web.mypetstore.persistence.ItemDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class CartDaoImpl implements CartDao {
    private static final String GET_CART_LIST="SELECT ItemId1,ItemQuantity1," +
            "ItemId2,ItemQuantity2," +
            "ItemId3,ItemQuantity3," +
            "ItemId4,ItemQuantity4," +
            "ItemId5,ItemQuantity5," +
            "ItemId6,ItemQuantity6 " +
            "FROM CART " +
            "WHERE Userid=?";
    private static final String UPDATE_CART_INF="UPDATE CART SET " +
            "ItemId1=?,ItemQuantity1=?," +
            "ItemId2=?,ItemQuantity2=?," +
            "ItemId3=?,ItemQuantity3=?," +
            "ItemId4=?,ItemQuantity4=?," +
            "ItemId5=?,ItemQuantity5=?," +
            "ItemId6=?,ItemQuantity6=? " +
            "WHERE Userid=?";

    @Override
    public List<CartItem> getCartList(String username){
        List<CartItem> cartItemList=null;
        Connection connection=null;
        try {
            connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET_CART_LIST);
            preparedStatement.setString(1, username);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                cartItemList=new ArrayList<>();
                for(int i=1;i<=6;i++){
                    if(!Objects.equals(resultSet.getString(2 * i-1), "")&&!Objects.equals(resultSet.getString(2 * i-1), "null")){
                        CartItem cartItem=new CartItem();
                        ItemDao itemDAO=new ItemDaoImpl();
                        cartItem.setItem(itemDAO.getItem(resultSet.getString(2 * i-1)));
                        cartItem.setQuantity(resultSet.getInt(2*i));
                        cartItemList.add(cartItem);
                    }else {
                        break;
                    }
                }
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return cartItemList;
    }

    @Override
    public void updateCartInf(List<CartItem> cartItemList, Account account) {
        Connection connection=null;
        try {
            connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_CART_INF);
            for (int i=0;i<6;i++){
                if(cartItemList.size()>i){
                    preparedStatement.setString(2*i+1, cartItemList.get(i).getItem().getItemId());
                    preparedStatement.setInt(2*i+2,cartItemList.get(i).getQuantity());
                }else{
                    preparedStatement.setString(2*i+1,"null");
                    preparedStatement.setInt(2*i+2,0);
                }
            }
            preparedStatement.setString(13,account.getUsername());
            preparedStatement.executeUpdate();
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
